<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>多个ajax请求的并发执行</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        li {
            list-style: none;
        }

        /* menu */
        .menu {
            width: 100px;
            background-color: rgba(0, 0, 0, 0.1);
            margin: 10px;
        }

        .menu-item {
            position: relative;
            padding: 5px;
            cursor: pointer;
        }

        .menu-content {
            display: none;
            position: absolute;
            left: 100%;
            top: 0;
            width: 200px;
            height: 100px;
            padding: 0 5px;
            background-color: rgba(0, 0, 0, 0.1);
        }

        .menu-item:hover {
            background-color: rgba(0, 0, 0, 0.4);
        }

        .menu-item:hover .menu-content {
            display: block;
        }

        .menu-loading {
            margin: 45px 0 0 92px;
        }

        /* loading-page */
        .loading-page {
            position: absolute;
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
            z-index: 1000;
            background-color: #eee;
            text-align: center;
        }

        .loading-img {
            position: absolute;
            top: 50%;
        }

        .ad img {
            display: inline-block;
            width: 25%;
        }

        .none {
            display: none;
        }
    </style>
</head>

<body>
    <div id="loading-page" class="loading-page">
        <img class="loading-img" src="./loading.gif" alt="加载中" />
    </div>

    <div id="ad" class="ad"></div>

    <ul id="menu" class="menu"></ul>
    <script type="module">
        import { getJSON } from './ajax/index.js';
        const menu = document.getElementById('menu');
        const ad= document.getElementById('ad');
        const loadingpage= document.getElementById('loading-page');
        const menuURL = 'https://www.imooc.com/api/mall-PC/index/menu';
        const adURL = 'https://www.imooc.com/api/mall-PC/index/ad';
        let html = '';
     const p1= getJSON(menuURL).then(response => {
            console.log(response)
            for (const item of response.data) {
                html += `<li class="menu-item" data-key="${item.key}" >
        <span>${item.title}</span>
        <div class="menu-content">
          <p><img class="menu-loading" src="./loading.gif" alt="加载中" /></p>
        </div>
      </li>`
            }
            menu.innerHTML=html;
        }).then(response=>{
            const items=menu.querySelectorAll('.menu-item');
            for(const item of items){
                item.addEventListener('mouseenter',()=>{
                    if(item.dataset.done==='done')return;
                    let html='';
                    getJSON(menuURL+`/${item.dataset.key}`).then(response=>{
                        item.dataset.done='done'
                        console.log(response)
                        for(const item of response.data){
                            html+=`<p>${item.title}`
                        }
                        item.querySelector('.menu-content').innerHTML=html;
                    })
                },false)
            }
        })
       const p2= getJSON(adURL).then(response=>{
            let html1='';
            console.log(response)
            for(const itme of response.data){
                html1+=`<img src="${itme.url}">`;
            }
            ad.innerHTML=html1;
        })
        Promise.all([p1,p2]).then(()=>{
            loadingpage.classList.add('none')
        });
    </script>
</body>

</html>